package com.itdianzi.controller.factory;


import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itdianzi.controller.base.BaseController;
import com.itdianzi.domain.cargo.Factory;
import com.itdianzi.domain.cargo.FactoryExample;
import com.itdianzi.service.cargo.FactoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/cargo/factory")
public class FactoryController extends BaseController {

    @Reference
    private FactoryService factoryService;

    /**
     * 跳转到生产厂家列表页面
     *
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria factoryExampleCriteria = factoryExample.createCriteria();
        factoryExampleCriteria.andCtypeBetween("货物", "附件");
        PageInfo pageInfo = factoryService.findAll(page, size, factoryExample);
        request.setAttribute("page", pageInfo);
        return "cargo/factory/factory-list";
    }

    /**
     * 跳转到添加页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd() {
        return "cargo/factory/factory-add";
    }

    /**
     * 进行新增或修改操作
     * @param factory
     * @return
     */
    @RequestMapping("/edit")
    public String edit(Factory factory) {
        if (StringUtils.isBlank(factory.getId())) {
            //设置状态
            factory.setState(0);
            factoryService.save(factory);
        } else {
            factoryService.update(factory);
        }
        return "redirect:/cargo/factory/list.do ";
    }

    /**
     * 跳转到查看厂家信息页面
     * @param id
     * @return
     */
    @RequestMapping("/toView")
    public String toView(String id) {
        //根据传递过来的ID  进行查询厂家信息
        Factory factory = factoryService.findById(id);
        //将厂家信息保存到request域中  到页面展示
        request.setAttribute("factory",factory);
        return "cargo/factory/factory-view";
    }

    /**
     * 跳转到修改厂家信息页面
     * @param id
     * @return
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(String id) {
        //根据厂家ID查询厂家信息
        Factory factory = factoryService.findById(id);
        //将获取到的厂家信息保存到request域中 到页面展示
        request.setAttribute("factory",factory);
        return "cargo/factory/factory-update";
    }

    /**
     * 进行厂家删除操作
     * @param id
     * @return
     */
    @RequestMapping("/delete")
    public String delete(String id) {
        //根据ID查询厂家信息
        Factory factory = factoryService.findById(id);
        //将厂家状态修改为2  就代表厂家与公司取消合作了
        factory.setState(2);
        //保存到数据库
        factoryService.update(factory);
        return "redirect:/cargo/factory/list.do ";
    }

    /**
     * 提交
     * @param id
     * @return
     */
    @RequestMapping("/submit")
    public String submit(String id) {
        Factory factory = factoryService.findById(id);
        //将厂家状态值改为1
        if (factory.getState() == 0) {
            factory.setState(1);
        }
        //保存到数据库
        factoryService.update(factory);
        return "redirect:/cargo/factory/list.do";
    }

    @RequestMapping("/cancel")
    public String cancel(String id) {
        Factory factory = factoryService.findById(id);
        //将厂家状态值改为1
        if (factory.getState() == 1) {
            factory.setState(0);
        }
        //保存到数据库
        factoryService.update(factory);
        return "redirect:/cargo/factory/list.do";
    }
}

